package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dtos.LoginDto;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.sms.SendSmsUtil;
import com.itheima.reggie.utils.RedisUtils;
import com.itheima.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;

/**
 * @author ghy
 * @version v1.0
 * create on 2022/11/21 15:55
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private SendSmsUtil sendSmsUtil;

    @Autowired
    private UserService userService;

    /**
     * 登录
     * @param session
     * @param loginDto
     * @return
     *//*
    @PostMapping("/login")
    public R login(HttpSession session, @RequestBody LoginDto loginDto){
        Long userId = userService.login(loginDto, (String)session.getAttribute("app_login_code:" + loginDto.getPhone()));
        //登录成功，把用户ID放进去
        session.setAttribute("user", userId);
        return R.success("用户登录成功");
    }*/

    /**
     * 登录
     * @param session
     * @param loginDto
     * @return
     */
    @PostMapping("/login")
    public R login(HttpSession session, @RequestBody LoginDto loginDto){
        // 1.获取验证码
        // 1.1 创建redis的key
        String key = RedisUtils.createKey("app_login_code", loginDto.getPhone());
        //1.2 获取redis中的验证码
        String redisCode = stringRedisTemplate.opsForValue().get(key);
        // 2.登录
        Long userId = userService.login(loginDto, redisCode);
        // 登录成功，把用户ID放进去
        session.setAttribute("user", userId);
        // 3.登录成功，删除验证码
        stringRedisTemplate.delete(key);
        return R.success("用户登录成功");
    }

/*
    *//**
     * 发送验证码
     * @param session
     * @param phone
     * @return
     * @throws Exception
     *//*
    @PostMapping("/sendMsg")
    public R send(HttpSession session, @RequestBody User user) throws Exception {
        //1.生成验证码并保存Session
        String code = ValidateCodeUtils.generateCode(4);
        session.setAttribute("app_login_code:" + user.getPhone(), code);
        //2.发送验证码
        //sendSmsUtil.sendMessage(phone, code, "SMS_254910142");
        log.warn("{}", code);
        return R.success(code);
    }*/


    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 发送验证码
     * @param session
     * @param phone
     * @return
     * @throws Exception
     */
    @PostMapping("/sendMsg")
    public R send(@RequestBody User user) throws Exception {
        //1.生成验证码并保存Redis
        String code = ValidateCodeUtils.generateCode(4);
        //创建redis的key
        String key = RedisUtils.createKey("app_login_code", user.getPhone());
        //存入redis，指定过期时间为5分钟
        stringRedisTemplate.opsForValue().set(key,code, 5, TimeUnit.MINUTES);
        //2.发送验证码
        //sendSmsUtil.sendMessage(phone, code, "SMS_254910142");
        log.warn("{}", code);
        return R.success(code);
    }

}
